// 用于存储注册用户信息的数组
let users = [];

window.onload = function () {
    var user = document.getElementsByClassName('user')[0];
    user.onclick = function () {
        // 创建登录框元素
        var loginBox = document.createElement('div');
        loginBox.style.position = 'fixed';
        loginBox.style.top = '50%';
        loginBox.style.left = '50%';
        loginBox.style.transform = 'translate(-50%, -50%)';
        loginBox.style.zIndex = '9999';
        loginBox.style.backgroundColor = 'white';
        loginBox.style.width = '300px';
        loginBox.style.height = '250px';
        loginBox.style.padding = '20px';
        loginBox.style.border = '1px solid gray';
        loginBox.style.textAlign = 'left';

        // 创建表单元素
        var form = document.createElement('form');

        // 登录提示信息
        var tip = document.createElement('p');
        tip.textContent = '登录';
        form.appendChild(tip);

        // 用户名输入框相关
        var usernameLabel = document.createElement('label');
        usernameLabel.textContent = '用户名: ';
        usernameLabel.style.display = 'block';
        var usernameInput = document.createElement('input');
        usernameInput.type = 'text';
        usernameInput.placeholder = '请输入用户名';
        usernameInput.style.width = '260px';
        usernameInput.style.height = '30px';
        usernameInput.style.marginTop = '5px';
        form.appendChild(usernameLabel);
        form.appendChild(usernameInput);

        // 密码输入框相关
        var passwordLabel = document.createElement('label');
        passwordLabel.textContent = '密码: ';
        passwordLabel.style.display = 'block';
        var passwordInput = document.createElement('input');
        passwordInput.type = 'password';
        passwordInput.placeholder = '请输入密码';
        passwordInput.style.width = '260px';
        passwordInput.style.height = '30px';
        passwordInput.style.marginTop = '5px';
        form.appendChild(passwordLabel);
        form.appendChild(passwordInput);

        // 注册按钮
        var registerButton = document.createElement('input');
        registerButton.type = 'button';
        registerButton.value = '注册';
        registerButton.style.marginTop = '15px';

        // 确定按钮
        var okButton = document.createElement('input');
        okButton.type = 'submit';
        okButton.value = '确定';
        okButton.style.marginRight = '10px';

        // 取消按钮
        var cancelButton = document.createElement('input');
        cancelButton.type = 'button';
        cancelButton.value = '取消';
        cancelButton.style.marginTop = '15px';


        // 将元素添加到表单
        form.appendChild(registerButton);
        form.appendChild(okButton);
        form.appendChild(cancelButton);


        // 将表单添加到登录框
        loginBox.appendChild(form);


        // 将登录框添加到页面
        document.body.appendChild(loginBox);


        // 处理取消按钮点击事件
        cancelButton.onclick = function () {
            document.body.removeChild(loginBox);
        };


        // 处理确定按钮点击事件
        okButton.onclick = function (e) {
            e.preventDefault();
            let foundUser = false;
            for (let i = 0; i < users.length; i++) {
                if (users[i].username === usernameInput.value && users[i].password === passwordInput.value) {
                    foundUser = true;
                    break;
                }
            }
        if (foundUser) {
            document.body.removeChild(loginBox);
            // window.location.href = "booking.html";
            window.open('personal.html','_blank')
        } else {
            alert('用户名或密码错误');
        }
        };


        // 处理注册按钮点击事件
        registerButton.onclick = function () {
            let newUser = {
                username: usernameInput.value,
                password: passwordInput.value
            };
            if (newUser.username && newUser.password) {
                users.push(newUser);
                document.body.removeChild(loginBox);
            } else {
                alert('请输入用户名和密码进行注册');
            }
        };
    };
};